package com.cskaoyan.day14;

public class EqulasTest {
    public static void main(String[] args) {
        Animal animal = new Animal(18, "18", 18);
        Animal animal2 = new Animal(18, "18", 18);
        AnimalSon animalSon = new AnimalSon(18, "18", 18);
        Cat cat = new Cat(18, "18", 18);
        Cat cat2 = new Cat(18, "18", 18);
        CatSon catSon = new CatSon(18, "18", 18);

        System.out.println("animal.equals(animal2) = " + animal.equals(animal2));
        System.out.println("animal.equals(animalSon) = " + animal.equals(animalSon));
        System.out.println("cat.equals(cat2) = " + cat.equals(cat2));
        System.out.println("cat.equals(catSon) = " + cat.equals(catSon));
        System.out.println("catSon.equals(cat) = " + catSon.equals(cat));
    }
}

class Animal {
    int age;
    String name = "";
    double price;

    public Animal(int age, String name, double price) {
        this.age = age;
        this.name = name;
        this.price = price;
    }

    public Animal() {
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || obj.getClass() != getClass()) {
            return false;
        }
        Animal animal = (Animal) obj;
        if (age == animal.age && name.equals(animal.name) && Double.compare(price, animal.price) == 0) {
            return true;
        }
        return false;
    }
}

class AnimalSon extends Animal {
    public AnimalSon(int age, String name, double price) {
        super(age, name, price);
    }
}

class Cat {
    int age;
    String name = "";
    double price;

    public Cat(int age, String name, double price) {
        this.age = age;
        this.name = name;
        this.price = price;
    }

    public Cat() {
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof Cat)) {
            return false;
        }
        Cat cat = (Cat) obj;
        if (age == cat.age && name.equals(cat.name) && Double.compare(price, cat.price) == 0) {
            return true;
        }
        return false;
    }
}

class CatSon extends Cat {
    public CatSon(int age, String name, double price) {
        super(age, name, price);
    }

    public CatSon() {
    }
}

